﻿#include "WidgetReadRequest.h"
#include <QFormLayout>
#include <QLabel>
#include <QInputDialog>

#pragma execution_character_set("utf-8")

WidgetReadRequest::WidgetReadRequest(QWidget *parent)
    : QWidget{parent}
{
    setupUi();
    initConnect();
}

void WidgetReadRequest::setRequestData(const PLCRequestData &data)
{
    spboxReadAddrStart->setValue(data.addrStart);
    cbboxRegisterType->setCurrentIndex(cbboxRegisterType->findData(data.table));
    lstwgtAddrList->clear();
    for(auto value : data.data){
        QString txt = valueTypeString.value(value.valueType);
        lstwgtAddrList->addItem(txt);
    }
}

PLCRequestData WidgetReadRequest::getRequestData()
{
    PLCRequestData readCfg;
    readCfg.addrStart = spboxReadAddrStart->value();
    readCfg.table = static_cast<QModbusDataUnit::RegisterType>(cbboxRegisterType->currentData().toInt());
    int index = 0;
    for(int i = 0; i < lstwgtAddrList->count(); ++i){
        QString curText = lstwgtAddrList->item(i)->text();
        ModbusValue value;
        value.valueType = static_cast<ValueType>(valueTypeString.values().indexOf(curText));
        value.addr = index;
        if(value.valueType == VT_Int16 || value.valueType == VT_Bool){
            index += 1;
        } else if(value.valueType == VT_Float || value.valueType == VT_Int32){
            index += 2;
        }
        readCfg.data << value;
    }
    return readCfg;
}

void WidgetReadRequest::setupUi()
{
    cbboxRegisterType = new QComboBox();
    spboxReadAddrStart = new QSpinBox();
    pbtnAddrAdd = new QPushButton("添加");
    pbtnAddrRemove = new QPushButton("删除");
    pbtnAddrRemoveAll = new QPushButton("删除全部");
    lstwgtAddrList = new QListWidget();

    cbboxRegisterType->addItem("保持寄存器", QModbusDataUnit::HoldingRegisters);
    cbboxRegisterType->addItem("线圈", QModbusDataUnit::InputRegisters);
    cbboxRegisterType->addItem("输入寄存器", QModbusDataUnit::Coils);

    spboxReadAddrStart->setRange(0, 50000);
    spboxReadAddrStart->setValue(100);
    lstwgtAddrList->setDragEnabled(true);
    lstwgtAddrList->setDragDropMode(QAbstractItemView::InternalMove);

    QFormLayout* fLayout01 = new QFormLayout();
    this->setLayout(fLayout01);
    fLayout01->addRow("线圈类型", cbboxRegisterType);
    fLayout01->addRow("读取地址", spboxReadAddrStart);
    QHBoxLayout* hLayoutAddr = new QHBoxLayout();

    QVBoxLayout* vLayoutCtrl = new QVBoxLayout();
    vLayoutCtrl->addWidget(pbtnAddrAdd);
    vLayoutCtrl->addWidget(pbtnAddrRemove);
    vLayoutCtrl->addWidget(pbtnAddrRemoveAll);
    vLayoutCtrl->addStretch();

    hLayoutAddr->addWidget(lstwgtAddrList);
    hLayoutAddr->addLayout(vLayoutCtrl);

    fLayout01->addRow(hLayoutAddr);
}

void WidgetReadRequest::initConnect()
{
    connect(pbtnAddrAdd, &QPushButton::clicked, this, [&](){
        QString type = getTxt(0);
        lstwgtAddrList->addItem(type);
    });
    connect(pbtnAddrRemove, &QPushButton::clicked, this, [&](){
        lstwgtAddrList->takeItem(lstwgtAddrList->currentRow());
    });
    connect(pbtnAddrRemoveAll, &QPushButton::clicked, this, [&](){
        lstwgtAddrList->clear();
    });
    connect(lstwgtAddrList, &QListWidget::doubleClicked, this, [&](const QModelIndex& index){
        int row = index.row();
        QString curText = lstwgtAddrList->item(row)->text();
        int type = valueTypeString.values().indexOf(curText);
        QString typeStr = getTxt(type);
        lstwgtAddrList->item(row)->setText(typeStr);
    });
}
QString WidgetReadRequest::getTxt(int index)
{
    return QInputDialog::getItem(this, "选择数据类型", "类型", valueTypeString.values(), index, false);;
}
